package com.kexilo.core.common.core.web.domain;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;

/**
 * API响应包装器
 * 
 * @author Kexilo
 */
@Data
@Schema(description = "API统一响应对象")
public class ApiResponse<T> implements Serializable {
    
    private static final long serialVersionUID = 1L;

    @Schema(description = "响应码", example = "200")
    private int code;

    @Schema(description = "响应消息", example = "操作成功")
    private String msg;

    @Schema(description = "响应数据")
    private T data;

    @Schema(description = "响应时间戳", example = "1640995200000")
    private long timestamp;

    @Schema(description = "请求ID", example = "req_123456789")
    private String requestId;

    @Schema(description = "API版本", example = "v1.0")
    private String version;

    public ApiResponse() {
        this.timestamp = System.currentTimeMillis();
        this.version = "v1.0";
    }

    public ApiResponse(int code, String msg) {
        this();
        this.code = code;
        this.msg = msg;
    }

    public ApiResponse(int code, String msg, T data) {
        this(code, msg);
        this.data = data;
    }

    /**
     * 成功响应
     */
    public static <T> ApiResponse<T> success() {
        return new ApiResponse<>(200, "操作成功");
    }

    /**
     * 成功响应（带数据）
     */
    public static <T> ApiResponse<T> success(T data) {
        return new ApiResponse<>(200, "操作成功", data);
    }

    /**
     * 成功响应（自定义消息）
     */
    public static <T> ApiResponse<T> success(String msg, T data) {
        return new ApiResponse<>(200, msg, data);
    }

    /**
     * 失败响应
     */
    public static <T> ApiResponse<T> error() {
        return new ApiResponse<>(500, "操作失败");
    }

    /**
     * 失败响应（自定义消息）
     */
    public static <T> ApiResponse<T> error(String msg) {
        return new ApiResponse<>(500, msg);
    }

    /**
     * 失败响应（自定义错误码和消息）
     */
    public static <T> ApiResponse<T> error(int code, String msg) {
        return new ApiResponse<>(code, msg);
    }

    /**
     * 参数错误响应
     */
    public static <T> ApiResponse<T> badRequest(String msg) {
        return new ApiResponse<>(400, msg);
    }

    /**
     * 未授权响应
     */
    public static <T> ApiResponse<T> unauthorized() {
        return new ApiResponse<>(401, "未授权访问");
    }

    /**
     * 禁止访问响应
     */
    public static <T> ApiResponse<T> forbidden() {
        return new ApiResponse<>(403, "禁止访问");
    }

    /**
     * 资源不存在响应
     */
    public static <T> ApiResponse<T> notFound() {
        return new ApiResponse<>(404, "资源不存在");
    }

    /**
     * 请求过于频繁响应
     */
    public static <T> ApiResponse<T> tooManyRequests() {
        return new ApiResponse<>(429, "请求过于频繁");
    }

    /**
     * 判断是否成功
     */
    public boolean isSuccess() {
        return this.code == 200;
    }

    /**
     * 设置请求ID
     */
    public ApiResponse<T> requestId(String requestId) {
        this.requestId = requestId;
        return this;
    }

    /**
     * 设置版本
     */
    public ApiResponse<T> version(String version) {
        this.version = version;
        return this;
    }
}
